﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using Nabu.Forms.Html;
using Nabu.Collections;

namespace dota2itemchanger
{
    public partial class MainForm : HtmlForm
    {
        HtmlTag lstAllHeros = null;
        HtmlTag currenthero = null;

        Hero selectedhero = null;

        public MainForm()
        {
            InitializeComponent();
            Items.Load();
            Heroes.Load();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            lstAllHeros = RootTag.SelectFirst("#all-heroes");
            currenthero = RootTag.SelectFirst("#current-hero");

            Subscribe("#all-heroes", OnChangeHero);
            Subscribe("#save-items", OnSaveItems);
            Subscribe("#reset", OnResetItems);

            LoadShops();

            foreach (Hero h in Heroes.AllHeroes())
            {
                HtmlTag option = HtmlTag.Create("option", h.name);
                option.Attributes.Add("value", h.name);
                lstAllHeros.AppendHtml(option.OuterHtml);
            }
        }

        void LoadShops()
        {
            Dictionary<string, List<Item>> items = Items.GetItems();
            HtmlTag shops = RootTag.SelectFirst(".shops");
            foreach (string s in items.Keys)
            {
                HtmlTag shop = HtmlTag.Create("div");
                shops.Children.Add(shop);
                shop.Attributes["class"] = "shop";

                HtmlTag shopname = HtmlTag.Create("div");
                shop.Children.Add(shopname);
                shopname.Attributes["class"] = "shop-name";
                shopname.InnerText = s;

                HtmlTag divitems = HtmlTag.Create("div");
                shop.Children.Add(divitems);
                divitems.Attributes["class"] = "items";
                
                foreach (Item i in items[s])
                {
                    HtmlTag img = HtmlTag.Create("img");
                    divitems.Children.Add(img);
                    img.Attributes["class"] = "item";
                    img.Attributes["src"] = i.RealImage();
                    img.Attributes["title"] = i.name;
                }
            }
        }

        void OnClose(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
        }

        private void OnChangeHero(object sender, HtmlBehaviorEventArgs e)
        {
            if (e.Command == HtmlBehaviorEventCommand.SelectSelectionChanged)
            {
                string newname = e.Source.InnerText;
                selectedhero = Heroes.GetHero(newname);
                currenthero.Attributes["src"] = selectedhero.RealImage();
                RootTag.SelectFirst("#save-items").InnerText = "Save items for " + newname;
            }
        }

        private void OnSaveItems(object sender, HtmlBehaviorEventArgs e)
        {
            if (e.Command == HtmlBehaviorEventCommand.ButtonClick)
            {
                if (selectedhero == null)
                {
                    MessageBox.Show("Please select a hero first.");
                    return;
                }
                DisposableList<HtmlTag> starting = RootTag.SelectFirst("#starting").Select("img");
                DisposableList<HtmlTag> early = RootTag.SelectFirst("#early").Select("img");
                DisposableList<HtmlTag> core = RootTag.SelectFirst("#core").Select("img");
                DisposableList<HtmlTag> situational = RootTag.SelectFirst("#situational").Select("img");

                if (starting.Count == 0)
                {
                    MessageBox.Show("Warning: You didn't set any starting items");
                }
                if (early.Count == 0)
                {
                    MessageBox.Show("Warning: You didn't set any early items");
                }
                if (core.Count == 0)
                {
                    MessageBox.Show("Warning: You didn't set any core items");
                }
                if (situational.Count == 0)
                {
                    MessageBox.Show("Warning: You didn't set any situational items");
                }

                string file = "default_" + selectedhero.image + ".txt";
                StreamWriter sw = new StreamWriter(file);
                sw.WriteLine("\"itembuilds/default_generic.txt\"");
                sw.WriteLine("{");
                sw.WriteLine("\t\"author\"\t\t\"Lamza\"");
                sw.WriteLine("\t\"hero\"\t\t\t\"npc_dota_hero_" + selectedhero.image + "\"");
                sw.WriteLine("\t\"title\"\t\t\t\"Generated!!!\"");
                sw.WriteLine("\t");
                sw.WriteLine("\t\"Items\"");
                sw.WriteLine("\t{");
                
                sw.WriteLine("\t\t\"#DOTA_Item_Build_Starting_Items\"");
                sw.WriteLine("\t\t{");
                foreach (HtmlTag t in starting)
                {
                    sw.WriteLine("\t\t\t\"item\"\t\t\"" + Items.GetItem(t.Attributes["title"]).image + "\"");
                }
                sw.WriteLine("\t\t}");
                sw.WriteLine("\t\t");

                sw.WriteLine("\t\t\"#DOTA_Item_Build_Early_Game\"");
                sw.WriteLine("\t\t{");
                foreach (HtmlTag t in early)
                {
                    sw.WriteLine("\t\t\t\"item\"\t\t\"" + Items.GetItem(t.Attributes["title"]).image + "\"");
                }
                sw.WriteLine("\t\t}");
                sw.WriteLine("\t\t");

                sw.WriteLine("\t\t\"#DOTA_Item_Build_Core_Items\"");
                sw.WriteLine("\t\t{");
                foreach (HtmlTag t in core)
                {
                    sw.WriteLine("\t\t\t\"item\"\t\t\"" + Items.GetItem(t.Attributes["title"]).image + "\"");
                }
                sw.WriteLine("\t\t}");
                sw.WriteLine("\t\t");

                sw.WriteLine("\t\t\"#DOTA_Item_Build_Luxury\"");
                sw.WriteLine("\t\t{");
                foreach (HtmlTag t in situational)
                {
                    sw.WriteLine("\t\t\t\"item\"\t\t\"" + Items.GetItem(t.Attributes["title"]).image + "\"");
                }
                sw.WriteLine("\t\t}");
                
                sw.WriteLine("\t}");
                sw.Write("}");

                sw.Close();
                
                MessageBox.Show("Created file: " + file);
            }
        }

        private void OnResetItems(object sender, HtmlBehaviorEventArgs e)
        {
            if (e.Command == HtmlBehaviorEventCommand.ButtonClick)
            {
                foreach (HtmlTag t in RootTag.Select(".item-content"))
                {
                    t.Children.Clear();
                }
            }
        }

    }
}
